package com.sendi.mobilework.pager.ext;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sendi.mobilework.R;
import com.sendi.mobilework.api.Api;
import com.sendi.mobilework.datamodel.Result;
import com.sendi.mobilework.datamodel.Workflow;
import com.sendi.mobilework.global.GlobalContent;
import com.sendi.mobilework.pager.BasePager;
import com.sendi.mobilework.util.CacheUtils;
import com.sendi.mobilework.util.HttpUtils;
import com.sendi.mobilework.util.Utils;
import com.sendi.mobilework.view.RefreshListView;
import com.sendi.mobilework.view.SwipeLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 工作流程页面
 * Created by Administrator on 2017/3/22.
 */

public class WorkflowManagerPager extends BasePager {
    private RefreshListView mListView;
    private List<Workflow.RecordBean> mWorkflowList;
    private FlowAdapter mAdapter;
    private ArrayList<SwipeLayout>mSlList;
    private ViewHolder holder = null;
    private View view = null;
    private String TAG="ASENDI";
    private final String ACTION="findApply";

    private Api mApi;

    public WorkflowManagerPager(Activity activity) {
        super(activity);

        Retrofit retrofit = HttpUtils.getRetrofit(GlobalContent.WORKERFLOW_MANAGER);
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void initView() {
        super.initView();
        mTitleText.setText("流程");
        View view = View.inflate(mActivity, R.layout.workflow_layout, null);
        mListView = (RefreshListView) view.findViewById(R.id.workflow_listview);
        mListView.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {//刷新数据
                getDatafromServelet();
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i(TAG, "onItemClick: "+position);
            }
        });

        mFrameLayout.addView(view);
    }

    @Override
    public void initData() {
        mWorkflowList = new ArrayList<>();
        mSlList=new ArrayList<>();
        mAdapter = new FlowAdapter();
        String cache=CacheUtils.getCache(mActivity,GlobalContent.WORKERFLOW_MANAGER+ACTION);
        if (!TextUtils.isEmpty(cache)){//获取缓存数据
            Gson gson=new Gson();
            mWorkflowList=gson.fromJson(cache, Workflow.class).getRecord();
            Log.i(TAG, "initData: "+cache);
            if (mWorkflowList!=null){
                mListView.setAdapter(mAdapter);
            }
        }else {
            getDatafromServelet();
        }

    }

    /**
     * 从网络获取数据
     */
    public void getDatafromServelet() {
        mApi.getWorkflowManager().enqueue(new Callback<Workflow>() {
            @Override
            public void onResponse(Response<Workflow> response) {
                mWorkflowList = response.body().getRecord();
                Gson gson=new Gson();
                Log.i("SENDI", "onResponse: " + mWorkflowList.get(1).getStatus());
                Log.i("SENDI", "onResponse: " + mWorkflowList.get(1).getWorker_name());
                Log.i("SENDI", "onResponse: " + mWorkflowList.get(1).apply_time);
                Log.i("SENDI", "onResponse: "+gson.toJson(response.body()));
                if (response.isSuccess()){
                    //缓存数据
                    CacheUtils.setCache(mActivity,GlobalContent.WORKERFLOW_MANAGER+ACTION,
                            gson.toJson(response.body()));
                    mListView.setAdapter(mAdapter);
                    mListView.refreshComplete(true);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.i(TAG, "onFailure: "+t.toString());
                mListView.refreshComplete(false);
            }
        });
    }


    class FlowAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mWorkflowList.size();
        }

        @Override
        public Workflow.RecordBean getItem(int position) {
            return mWorkflowList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, final View convertView, ViewGroup parent) {

            if (convertView == null) {
                view = View.inflate(mActivity, R.layout.workflow_item, null);
                holder = new ViewHolder();
                holder.applyName = (TextView) view.findViewById(R.id.tv_name);
                holder.department = (TextView) view.findViewById(R.id.tv_department);
                holder.reason = (TextView) view.findViewById(R.id.tv_reason);
                holder.time = (TextView) view.findViewById(R.id.tv_time);
                holder.status = (TextView) view.findViewById(R.id.bt_idea);
                holder.mSwipeLayout= (SwipeLayout) view.findViewById(R.id.sl);
                holder.agree= (TextView) view.findViewById(R.id.tv_agree);
                holder.disagree= (TextView) view.findViewById(R.id.tv_disagree);
                view.setTag(holder);
            } else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }
            holder.department.setText(mWorkflowList.get(position).department);
            holder.applyName.setText(mWorkflowList.get(position).worker_name);
            holder.reason.setText(mWorkflowList.get(position).content);
            holder.time.setText(mWorkflowList.get(position).apply_time);

            holder.status.setText(mWorkflowList.get(position).status);


            holder.agree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utils.showToast(mActivity,"同意");
                    changeStatus(position,true);
                    mWorkflowList.get(position).status="同意";
                    notifyDataSetChanged();

                }
            });
            holder.disagree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utils.showToast(mActivity,"不同意");
                    changeStatus(position,false);
                    mWorkflowList.get(position).status="不同意";
                    notifyDataSetChanged();
                }
            });

            holder.mSwipeLayout.setOnSwipeLayoutListener(new SwipeLayout.OnSwipeLayoutListener() {
                @Override
                public void onClose(SwipeLayout swipeLayout) {
                    //去除关闭的swipeLayout
                    mSlList.remove(swipeLayout);
                }

                @Override
                public void onOpen(SwipeLayout swipeLayout) {
                    //添加关闭的swipeLayout
                    mSlList.add(swipeLayout);
                }

                @Override
                public void onDraging(SwipeLayout swipeLayout) {

                }

                @Override
                public void onStartClose(SwipeLayout swipeLayout) {

                }

                @Override
                public void onStartOpen(SwipeLayout swipeLayout) {
                    //先遍历已打开的条目，然后在逐个关闭
                    for (SwipeLayout swipeLayout1:mSlList){
                        swipeLayout1.close(true);
                    }
                    mSlList.clear();
                }
            });

            return view;
        }
    }

    private void changeStatus(int position,boolean isAgree) {
       String id= mWorkflowList.get(position).id;
        if (isAgree){
            //同意
            mApi.updateStatus(id,"1").enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Response<Result> response) {
                    if (response.isSuccess()){
                        String result=  response.body().getResult();
                        Log.i(TAG, "onResponse: "+result);
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    Log.i(TAG, "onFailure: "+t.getMessage());
                }
            });
        }else {
            //不同意
            mApi.updateStatus(id,"0").enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Response<Result> response) {
                    if (response.isSuccess()){
                        String result=  response.body().getResult();
                        Log.i(TAG, "onResponse: "+result);
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    Log.i(TAG, "onFailure: "+t.getMessage());
                }
            });
        }
    }

    class ViewHolder {
        public TextView department;
        public TextView reason;
        public TextView applyName;
        public TextView time;
        public TextView status;
        public SwipeLayout mSwipeLayout;
        public TextView agree;
        public TextView disagree;
    }
}
